package com.hs.service;

import com.hs.config.PageObj;
import com.hs.dao.YxModelMapper;
import com.hs.entity.YxModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class YxModelService {
    @Autowired
    YxModelMapper yxModelMapper;
    @Transactional
    public int insertSelective(YxModel record){
        if(record.getImgUrl() == null){
            record.setImgUrl("/img/default.png");
        }
        record.setCreateTime(new Date());
        int i = yxModelMapper.insertSelective(record);
        if(i == 0){
            throw new RuntimeException("插入失败");
        }
        return  i;
    };
    public int updateByPrimaryKeySelective(YxModel record){
        return yxModelMapper.updateByPrimaryKeySelective(record);
    }

    public List<YxModel> selectByCondition(YxModel record){
        return yxModelMapper.selectByCondition(record);
    };
    public YxModel selectByPrimaryKey(Integer id){
        return yxModelMapper.selectByPrimaryKey(id);
    };

    public PageObj page(YxModel item){
        int total = yxModelMapper.selectByConditionCount(item);
        if(total == 0){
            return new PageObj(item.getCurrentPage(), item.getPageSize(),total,null);
        }
        List<YxModel> list = yxModelMapper.selectByCondition(item);
        return new PageObj(item.getCurrentPage(), item.getPageSize(),total,list);
    }
    public int selectByConditionCount(YxModel record){
        int count =  yxModelMapper.selectByConditionCount(record);
        return count;
    }


}
